<?php
namespace Home\Controller;

use Think\Controller;

class CartsController extends CommonController
{
  //购物车--针对某个用户
  protected $this_table="carts";

  //TP构造函数,子类同时调用
  public function __construct()
  {
    parent::__construct();
    $this->check_login();
  }

  //创建信息
  public function add()
  {
    $model = M($this->this_table);
    $now_time = gmtime();
    $DB_PREFIX=C("DB_PREFIX");

    // $post_data = I('post.');

    //判断用户是否存在于购物车
    $where['user_id'] = $this->user_id;
    $cart_id=M($this->this_table)->where($where)->getField("id");
    if ($cart_id) {
      $post_data['updated_at'] = $now_time;
      $result = $model->where($where)->save($post_data);
    } else {
      $post_data['user_id'] = $this->user_id;
      $post_data['created_at'] = $now_time;
      $post_data['updated_at'] = $now_time;
      $cart_id = $model->add($post_data);
    }

    //商品放入购物车对应商品表--一件一件
    $product_id = I("post.product_id");
    $spec_id = I("post.spec_id", 0);//规格ID
    $num = I("post.num", 1);//num一般为1
    if (!$product_id) {
      return_data("error", $this->this_table.".product_id.blank");
    }

    //其实规格应该对应一个库存

    /************判断商品的有效性**********/
    $where="p.id=".$product_id;
    if ($spec_id) {
      $where.=" and s.id=".$spec_id;
    }
    $product_info = M("products")->alias("p")->join("left join ".$DB_PREFIX."specs s on s.product_id=p.id")
                   ->field("p.status,p.stock,p.product_num,p.shop_id,s.id as sid")->where($where)->find();
    //有属性但是找不到商品
    if (!$product_info['sid']&&$spec_id) {
      return_data("error", "specs.id.error");
    }
    //查看属性合法性
    elseif (empty($spec_id)) {
      $specs_num=M("specs")->where("product_id=".$product_id)->count();
      if ($specs_num>0) {
        return_data("error", "specs.id.error");
      }
    }
    //判断商品是否归属该商户中
    // if($this->shop_id != $product_info['shop_id']) return_data("error","products.shop_id.error");
    //判断商品是否上架
    if ($product_info['status'] != 1) {
      return_data("error", "products.status.error");
    }
    //判断商品库存是否足够

    if ($product_info['stock']<$num) {
      return_data("error", "products.stock.error");
    }
    /************判断商品的有效性**********/

    //判断购物车中是否存在该商品
    $where = array();
    $where['cart_id'] = $cart_id;
    $where['product_id'] = $product_id;
    $where['spec_id'] = $spec_id;
    $cart_products_id = M("cart_products")->where($where)->getField("id");
    if ($cart_products_id) {
      //商品数量加
      $where = array();
      $where['id'] = $cart_products_id;
      $post_data = array();
      $post_data['updated_at'] = $now_time;
      $post_data['num'] = array('exp','num+'.$num);
      $result = M("cart_products")->where($where)->save($post_data);
    } else {
      $post_data = array();
      $post_data['cart_id'] = $cart_id;
      $post_data['product_id'] = $product_id;
      $post_data['num']=$num;
      $post_data['spec_id'] = $spec_id;
      $post_data['created_at'] = $now_time;
      $post_data['updated_at'] = $now_time;
      $result = M("cart_products")->add($post_data);
      $cart_products_id=$result;
    }

    if (!$result) {
      return_data("error", "create.".$this->this_table.".error");//blank
    } else {
      return_data("success", "create.".$this->this_table.".success", $cart_products_id);
    }
  }

  //获取信息
  public function show()
  {
    $model = M($this->this_table);
    $now_time = gmtime();
    $DB_PREFIX=C("DB_PREFIX");
    $where['user_id'] = $this->user_id;
    $cart_id = M($this->this_table)->where($where)->getField("id");
    if (!$cart_id) {
      return_data("error", "show.".$this->this_table.".empty");
    }

    //购物车中商品
    $page_index = I("page_index", 1);
    $page_size = I("page_size", 10);

    $where=" cp.cart_id=".$cart_id;
    $order = "cp.id desc ";
    //商品价格=起始价格+规格价格
    //商品状态--商品ID---商品数量--商品价格
    $field = "cp.id,cp.product_id,cp.num as product_num,p.name as product_name,p.status,(s.price+p.price) as product_price,s.name as product_spec,p.stock ";
    $list = M("cart_products")->alias("cp")->join("left join ".$DB_PREFIX."products p  on p.id=cp.product_id")
                        ->join("left join ".$DB_PREFIX."specs s on s.id=cp.spec_id")
                        ->field($field)->where($where)->order($order)->page($page_index.",".$page_size)->select();
    if (empty($list)) {
      return_data("error", "show.".$this->this_table.".empty");
    } else {
      //规格--点餐不需要
      return_data("success", "show.".$this->this_table.".success", $list);
    }
  }

  //删除==购物车减少数量
  public function delete()
  {
    $model = M($this->this_table);
    $now_time = gmtime();

    //清空购物车？？--暂时不做


    //商品放入购物车对应商品表--一件一件
    $cart_products_id = I("post.id");
    $num = I("post.num", 1);//num一般为1


    //判断购物车中是否存在该商品
    $where = array();
    $where['id'] = $cart_products_id;
    $cart_products_info = M("cart_products")->field("id,cart_id,num")->where($where)->find();
    if (!$cart_products_info) {
      return_data("error", "cart_products.id.blank");
    }

    $cart_products_num = $cart_products_info['num'];

    //购物车归属何人
    $cart_id = $cart_products_info['cart_id'];
    $user_id=M("carts")->where("id=".$cart_id)->getField("user_id");
    if ($this->user_id!=$user_id) {
      return_data("error", "carts.id.error");
    }

    //商品数量
    $where = array();
    $where['id'] = $cart_products_id;
    //数量相减为0，删除商品
    if ($cart_products_num<=$num) {
      $result = M("cart_products")->where($where)->delete();
    } else {
      $post_data = array();
      $post_data['updated_at'] = $now_time;
      $post_data['num'] = array('exp','num-'.$num);
      $result = M("cart_products")->where($where)->save($post_data);
    }
    //没有商品--删除carts中用户记录--暂时不做

    if (!$result) {
      return_data("error", "delete.".$this->this_table.".error");//blank
    } else {
      return_data("success", "delete.".$this->this_table.".success", $cart_products_id);
    }
  }
}
